package cn.tju.leavingsystem.service.impl;

import cn.tju.leavingsystem.dao.LeavingApplicationDao;
import cn.tju.leavingsystem.entity.LeavingApplication;
import cn.tju.leavingsystem.service.LeavingApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LeavingApplicationServiceImpl implements LeavingApplicationService {

    @Autowired
    private LeavingApplicationDao leavingApplicationDao;
    /**
     * 得到库中所有的请假单
     *
     * @return 请假单列表
     */
    @Override
    public List<LeavingApplication> getLeavingAppliactionList() {
        return leavingApplicationDao.queryLeavingApplication();
    }

    /**
     * 通过id查找请假单
     *
     * @param applicationId 请假单id
     * @return 请假单
     */
    @Override
    public LeavingApplication getLeavingApplicationById(int applicationId) {
        return leavingApplicationDao.queryLeavingApplicationById(applicationId);
    }

    /**
     * 通过员工的id查找请假单
     *
     * @param staffId 员工id
     * @return 请假单
     */
    @Override
    public List<LeavingApplication> getLeavingApplicationByStaffId(int staffId) {
        return leavingApplicationDao.queryLeavingApplicationByStaffId(staffId);
    }

    /**
     * 填写申请单
     *
     * @param leavingApplication 申请单
     * @return 是否插入成功
     */
    @Override
    public boolean addLeavingApplication(LeavingApplication leavingApplication) {
        //空值判断
        if (!"".equals(leavingApplication.getStaffId()) &&
            leavingApplication.getStaffName() != null && !"".equals(leavingApplication.getStaffName()) &&
            leavingApplication.getHolidayStart() != null && !"".equals(leavingApplication.getHolidayStart()) &&
            leavingApplication.getHolidayEnd() != null && !"".equals(leavingApplication.getHolidayEnd()) &&
            leavingApplication.getType() != null && !"".equals(leavingApplication.getType()) &&
            leavingApplication.getReason() != null && !"".equals(leavingApplication.getReason())
        ){
            try{
                int effectedNum = leavingApplicationDao.insertLeavingApplication(leavingApplication);
                if (effectedNum > 0) {
                    return true;
                }else {
                    throw new RuntimeException("插入申请单失败");
                }
            }catch (Exception e){
                throw new RuntimeException("插入申请单失败:" + e.getMessage());
            }
        }else {
            throw new RuntimeException("必填信息不能为空");
        }
    }

    /**
     * 修改申请单
     *
     * @param leavingApplication 申请单
     * @return 是否修改成功
     */
    @Override
    public boolean modifyLeavingApplication(LeavingApplication leavingApplication) {
        if(!"".equals(leavingApplication.getApplicationId()) && leavingApplication.getApplicationId() > 0){
            try{
                int effectedNum = leavingApplicationDao.updateLeavingApplication(leavingApplication);
                if(effectedNum >0){
                    return true;
                }else{
                    throw new RuntimeException("修改申请单失败");
                }
            }catch (Exception e){
                throw new RuntimeException("修改申请单失败:" + e.getMessage());
            }
        }else{
            throw new RuntimeException("申请单信息不能为空");
        }
    }

    /**
     * 根据id删除申请单
     *
     * @param applicationId 申请单id
     * @return 是否删除成功
     */
    @Override
    public boolean removeLeavingApplication(int applicationId) {
        if(applicationId > 0){
            try{
                int effectNum = leavingApplicationDao.deleteLeavingApplication(applicationId);
                if(effectNum > 0){
                    return true;
                }else {
                    throw new RuntimeException("删除请假单失败");
                }
            }catch (Exception e){
                throw new RuntimeException("删除请假单失败" + e.getMessage());
            }
        }else{
            throw new RuntimeException("不存在该请假单");
        }
    }
}
